package designMode.proxyPattern.protect;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author LIBOFEI
 * @ClassName OwnerInvocationHandler
 * @description 动态代理类
 * @date 2020/6/30 17:01
 * @Version 1.0
 **/
public class NonOwnerInvocationHandler implements InvocationHandler {

    PersonBean personBean;

    public NonOwnerInvocationHandler(PersonBean personBean){
        this.personBean = personBean;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //如果是自己，只允许修改信息，不允许设置分数
        try{
            if(method.getName().equals("setHotOrNotRating")){
                return method.invoke(personBean,args);
            }else{
                throw new IllegalAccessException();
            }
        }catch (InvocationTargetException e){
            e.printStackTrace();
        }
        //如果调用其他方法，一概不理，返回null
        return null;
    }
}
